﻿// Copyright 2010 ESRI
// 
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
// 
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
// 
// See the use restrictions at &lt;your ArcGIS install location&gt;/DeveloperKit10.0/userestrictions.txt.
// 

using System;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.esriSystem;
using DIWAGIS.Modules;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using DIWAGIS.ApplicationLayer.QuanLyMangCapNuoc;


namespace DIWAGIS.ApplicationLayer.EditorTools
{
	[ClassInterface(ClassInterfaceType.None)]
	[Guid("3362ba66-bf63-442a-a639-d18d583a028d")]
	public sealed class SelectFeatures : BaseTool
	{
        #region COM Registration Function(s)
        [ComRegisterFunction()]
        [ComVisible(false)]
        static void RegisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryRegistration(registerType);

            //
            // TODO: Add any COM registration code here
            //
        }

        [ComUnregisterFunction()]
        [ComVisible(false)]
        static void UnregisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryUnregistration(registerType);

            //
            // TODO: Add any COM unregistration code here
            //
        }

        #region ArcGIS Component Category Registrar generated code
        /// <summary>
        /// Required method for ArcGIS Component Category registration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryRegistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            ControlsCommands.Register(regKey);

        }
        /// <summary>
        /// Required method for ArcGIS Component Category unregistration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryUnregistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            ControlsCommands.Unregister(regKey);

        }

        #endregion
        #endregion

        private IPoint m_pPoint;
        private INewEnvelopeFeedback m_pFeedbackEnv;
        private Boolean flat = false;
        private IHookHelper m_hookHelper;
        private System.Windows.Forms.Cursor m_CursorMove;
																	  
		public SelectFeatures()
		{
			//Create an IHookHelper object
            m_hookHelper = new HookHelperClass();

			//Set the tool properties
			base.m_caption = "Select Features";
			base.m_category = "Sample_Select(C#)";
			base.m_name = "Sample_Select(C#)_Select Features";
			base.m_message = "Selects Features By Rectangle Or Single Click";
			base.m_toolTip = "Selects Features";
			base.m_deactivate = true;
			base.m_bitmap = new System.Drawing.Bitmap(GetType().Assembly.GetManifestResourceStream(GetType(), "SelectFeatures.bmp"));
            m_CursorMove = new System.Windows.Forms.Cursor(GetType().Assembly.GetManifestResourceStream(GetType(), "SelectFeatures.cur"));
		}

		public override void OnCreate(object hook)
		{
            m_hookHelper.Hook = hook;

           
		}
	
		public override bool Enabled
		{
			get
			{
                if (m_hookHelper.FocusMap == null) return false;
                return (m_hookHelper.FocusMap.LayerCount > 0);
			}
		}

        public override void OnMouseDown(int Button, int Shift, int X, int Y)
        {
            IMap map;
            IActiveView pActiveView;
            IPoint clickedPoint = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);

            //If ActiveView is a PageLayout 
            if (m_hookHelper.ActiveView is IPageLayout)
            {
                //See whether the mouse has been clicked over a Map in the PageLayout 
                map = m_hookHelper.ActiveView.HitTestMap(clickedPoint);

                //If mouse click isn't over a Map exit 
                if (map == null)
                    return;


                //Ensure the Map is the FocusMap 
                if ((!object.ReferenceEquals(map, m_hookHelper.FocusMap)))
                {
                    m_hookHelper.ActiveView.FocusMap = map;
                    m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
                }

                //Still need to convert the clickedPoint into map units using the map's IActiveView 
                clickedPoint = ((IActiveView)map).ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
            }

            else //Or ActiveView is a Map 
            {
                map = m_hookHelper.FocusMap;
            }
            pActiveView = (IActiveView)m_hookHelper.FocusMap;

            pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);

            IActiveView activeView = (IActiveView)map;
            IRubberBand rubberEnv = new RubberEnvelopeClass();
            IGeometry geom = rubberEnv.TrackNew(activeView.ScreenDisplay, null);
            IArea area = (IArea)geom;

            //Extra logic to cater for the situation where the user simply clicks a point on the map 
            //or where envelope is so small area is zero 
            if ((geom.IsEmpty == true) || (area.Area == 0))
            {

                //create a new envelope 
                IEnvelope tempEnv = new EnvelopeClass();

                //create a small rectangle 
                ESRI.ArcGIS.esriSystem.tagRECT RECT = new tagRECT();
                RECT.bottom = 0;
                RECT.left = 0;
                RECT.right = 5;
                RECT.top = 5;

                //transform rectangle into map units and apply to the tempEnv envelope 
                IDisplayTransformation dispTrans = activeView.ScreenDisplay.DisplayTransformation;
                dispTrans.TransformRect(tempEnv, ref RECT, 4); //4 = esriDisplayTransformationEnum.esriTransformToMap)
                tempEnv.CenterAt(clickedPoint);
                geom = (IGeometry)tempEnv;
            }

            //Set the spatial reference of the search geometry to that of the Map 
            ISpatialReference spatialReference = map.SpatialReference;
            geom.SpatialReference = spatialReference;

            map.SelectByShape(geom, null, false);
            activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, activeView.Extent);

            //// bo sung

            IFeatureSelection pFeatSel;
            ISelectionSet pSelectionSet;
            ICursor pCursor;
            IFeature pFeat;

            ILayer layer = null;
            // lấy layer muốn tìm feature 


            for (int i = 0; i < generalVariable.mapControl.LayerCount - 1; i++)
            {
                layer = generalVariable.mapControl.get_Layer(i);



                pFeatSel = (IFeatureSelection)layer;
                pSelectionSet = pFeatSel.SelectionSet;

                if (pSelectionSet == null)
                {
                    break;
                }


                //pSelectionSet = pFeatureSelection.SelectionSet
                //If pSelectionSet Is Nothing Then Exit Sub
                //pSelectionSet.Search(Nothing, False, pFeatureCursor)



                pSelectionSet.Search(null, false, out pCursor);

                IFeatureCursor fCursor;

                fCursor = (IFeatureCursor)pCursor;
                pFeat = fCursor.NextFeature();

                if (pFeat != null)
                {


                    switch (layer.Name)
                    {
                        case "Đồng hồ khách hàng":
                            //MessageBox.Show(datatable.get_Value(datatable.Fields.FindField("ObjectID")).ToString());
                            frmDongHoKhachHang frmDHKH;
                            frmDHKH = new frmDongHoKhachHang();
                            frmDHKH.pFeature = pFeat;
                            frmDHKH.ShowDialog();
                            break;
                        case "Ống chính":
                            //MessageBox.Show("1");
                            frmOngChinh frmOPP;
                            frmOPP = new frmOngChinh();
                            frmOPP.pFeature = pFeat;
                            frmOPP.ShowDialog();
                            break;
                        case "Ống nhánh":
                            frmOngNhanh frmON;
                            frmON = new frmOngNhanh();
                            frmON.pFeature = pFeat;
                            frmON.ShowDialog();
                            break;

                        case "Van":
                            frmVan frmV;
                            frmV = new frmVan(pFeat);
                            frmV.pFeature = pFeat;
                            frmV.ShowDialog();
                            break;

                        case "Trụ cứu hỏa":
                            frmTruCuuHoa frmTruHong;
                            frmTruHong = new frmTruCuuHoa();
                            frmTruHong.pFeature = pFeat;
                            frmTruHong.ShowDialog();
                            break;
                        case "Đồng hồ tổng":
                            frmDongHoTong frmDHT;
                            frmDHT = new frmDongHoTong();
                            frmDHT.pFeature = pFeat;
                            frmDHT.ShowDialog();
                            break;
                        case "Ống nối":

                            frmOngNoi frmOngNoi;
                            frmOngNoi = new frmOngNoi();
                            frmOngNoi.pFeature = pFeat;
                            frmOngNoi.ShowDialog();
                            break;
                      

                        //case "Thửa":
                            //frmThua frmT;
                            //frmT = new frmThua();
                            //frmT.pFeature = pFeat;
                            //frmT.ShowDialog();
                            //break;

                        default:
                            //MessageBox.Show(datatable.get_Value(datatable.Fields.FindField("ObjectID")).ToString());

                          
                            break;

                    }
                }

            }





        }
	
		public override int Cursor
		{
			get
			{
				return m_CursorMove.Handle.ToInt32();
				
			}
		}
	}
}
